package com.jnu.keepaccounts.data;

import android.content.Context;
import android.util.Log;

import com.jnu.keepaccounts.R;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class XuanKaBank {
    private final Context context;
    private final String FILE_NAME = "xuanka";
    ArrayList<XuanKa> xuanKas = new ArrayList<>();
    public XuanKaBank(Context context){
        this.context = context;
    }
    public ArrayList<XuanKa> loadData() {
//        xuanKas.add(new XuanKa("其他",-1, R.drawable.others));
//        xuanKas.add(new XuanKa("吃喝饮食",-1, R.drawable.eat));
//        xuanKas.add(new XuanKa("交通出行",-1, R.drawable.traffic));
//        xuanKas.add(new XuanKa("欠债借款",-1, R.drawable.debt));
//        xuanKas.add(new XuanKa("通讯费用",-1, R.drawable.phone_fee));
//        xuanKas.add(new XuanKa("人情礼物",-1, R.drawable.present));
//        xuanKas.add(new XuanKa("房租电费",-1, R.drawable.rent));
//        xuanKas.add(new XuanKa("医疗费用",-1, R.drawable.medical));
//        xuanKas.add(new XuanKa("社交红包",-1, R.drawable.red_packet));
//        xuanKas.add(new XuanKa("逛街购物",-1, R.drawable.shopping));
//        xuanKas.add(new XuanKa("运动健身",-1, R.drawable.sport));
//        xuanKas.add(new XuanKa("工资薪水",1, R.drawable.salary));
//        xuanKas.add(new XuanKa("日常用品",-1, R.drawable.daily_supply));
//        xuanKas.add(new XuanKa("聚会聚餐",-1, R.drawable.party));
//        xuanKas.add(new XuanKa("基金理财",-1, R.drawable.financing));
//        xuanKas.add(new XuanKa("旅行旅游",-1, R.drawable.travel));







        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(FILE_NAME));
            xuanKas = (ArrayList<XuanKa>) objectInputStream.readObject();
            Log.d("xuanka","load done");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
       return xuanKas;
    }

    public void saveData(){
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(FILE_NAME,Context.MODE_PRIVATE));
            objectOutputStream.writeObject(xuanKas);
            Log.d("xuanka bank","save done");
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (objectOutputStream !=null){
                    objectOutputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
